"""

项目接口


"""








#1.导包


import re
from flask import Flask
import time
import json
#基础模块
from  flask import request,jsonify

#导入用户模块

#建立flask实例
app = Flask(__name__)

# 全局配置跨域
from flask_cors import CORS
cors = CORS(app)


#解决中文乱码
from config import Config
app.config.from_object(Config)




#注册蓝图
from lianxi.hoke import user

# app.register_blueprint(user,url_prefix = '/add')


"""
设置sqlalchemy 

"""
#导包
from flask_sqlalchemy import SQLAlchemy

#实例化
db = SQLAlchemy(app)


#创建表
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    name = db.Column(db.String(64), unique=True)
    sex = db.Column(db.Boolean,default=True)
    add_time = db.Column(db.DateTime)
    content = db.Column(db.Text)
    score = db.Column(db.Float)

#创建表
# db.create_all()

#删除表
# db.drop_all()

#添加数据
@app.route('/sqladd/',methods=['POST','GET','PUT','DELETE'])
def sqladd():
    if request.method == 'POST':
        data = request.form

        user = User(add_time=data['add_time'],content=data['content'],score=data['score'],name=data['name'])
        

        db.session.add(user)

        db.session.commit()

        return 'ok'
    elif request.method =='GET':
        print('***********************************')
        # # 查询全部
        # res = User.query.all()
        # for i in res:
        #     print(i.name)

        #查询1个 根据id
        # res = User.query.get(24)
        # print(res.name)

        #按条件查询
        res = User.query.filter_by(name='liu')
        print(res)
        return 'ok'
    elif request.method == 'PUT':
        id = request.args.get('id')
        #修改数据
        res = User.query.filter_by(id=id).first()
        res.content = '不会玩游戏'
        db.session.commit()
        return 'ok'
    else:
        id = request.args.get('id')
        User.query.filter_by(id=id).delete()
        db.session.commit()
        return 'ok'
    




#调用实例对象

if __name__ =='__main__':

    app.run(debug=True,host='0.0.0.0',port = 5000)  #开启调试模式  host开启外部访问别人可以访问

    








